package org.wkg.lesson02;

public class Demo_202 {
    private static long count = 10_0000_0000l;

    public static void main(String[] args) {

        // 串行
        serial();
        // 并行
        concurrency();


    }

    public static void serial() {
        long begin =System.currentTimeMillis();
        long a = 0l;
        for (int i = 0; i < count; i++) {
            a++;
        }
        long b = 0l;
        for (int i = 0; i < count; i++) {
            b++;
        }
        long end =System.currentTimeMillis();
        System.out.println("串行运行时间为：" + (end - begin));
    }

    public static void concurrency() {

        Thread thread1 = new Thread(() -> {
            long a = 0l;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        Thread thread2 = new Thread(() -> {
            long b = 0l;
            for (int i = 0; i < count; i++) {
                b++;
            }
        });
        double begin = System.currentTimeMillis();
        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        double end = System.currentTimeMillis();
        System.out.println("并行运行时间为：" + (end - begin));

    }
}


